/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.biz.front.repository.impl.ICommonDao.java
 * <p>
 * 类产生时间: 2014/3/26 20:36
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.common.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.oceansoft.mobile.biz.front.entity.Jp;
import com.oceansoft.mobile.biz.front.entity.JpMapper;


/**
 * 律师备案
 *
 * @author: chenw
 * @time: 2014/3/26 20:36
 */
@Repository
public class ExtendCommonDaoImpl implements IExtendCommonDao {
    
    @Resource(name="extendJdbcTemplate")
    private JdbcTemplate  ejt;
    
    @Resource(name="extendNamedParameterJdbcTemplate")
    private NamedParameterJdbcTemplate npjt;


    public <T> T loadOne(String sql, RowMapper<T> rowMapper, Object... params) {
        List<T> rt = ejt.query(sql, rowMapper, params);
        if (0 < rt.size())
            return rt.get(0);
        return null;
    }

    public <T> List<T> loadList(String sql, RowMapper<T> rowMapper, Object... params) {
        return ejt.query(sql, rowMapper, params);
    }
    public <T> List<T> loadList(String sql, RowMapper<T> rowMapper) {
    	return ejt.query(sql, rowMapper);
    }

    public int saveOne(String sql, Object object) {
    	KeyHolder keyHolder = new GeneratedKeyHolder();
        int rt = ejt.update(sql, new BeanPropertySqlParameterSource(object),keyHolder);
        if (rt > 0)
        	return keyHolder.getKey().intValue();
        return rt;
    }
    

    public int getCount(String sql, Object... params) {
        return ejt.queryForObject(sql, Integer.class, params);
    }
    
    public String getStringColumn(String sql,Object... params){
    	return ejt.queryForObject(sql, String.class, params);
    }
    public Integer getIntColumn(String sql,Object... params){
    	return ejt.queryForObject(sql, Integer.class, params);
    }

    public int updateColumn(String tableName, String filterColumnName, Object filterColumnValue, String updateColumnName, Object newColumnValue) {
        return ejt.update(String.format("UPDATE %1$s T SET T.%2$s = ? WHERE T.%3$s = ? ", tableName, updateColumnName, filterColumnName), newColumnValue, filterColumnValue);
    }
    public int updateColumn2(String tableName, String filterColumnName, Object filterColumnValue, String updateColumnName, Object newColumnValue, String updateColumnName2, Object newColumnValue2) {
    	return ejt.update(String.format("UPDATE %1$s T SET T.%2$s = ?,T.%3$s = ? WHERE T.%4$s = ? ", tableName, updateColumnName,updateColumnName2, filterColumnName), newColumnValue,newColumnValue2, filterColumnValue);
    }

    public int[] updateBatch(String sql, List<Object[]> objects) {
        return ejt.batchUpdate(sql, objects);
    }

    public int deleteOne(String sql, long id) {
        return ejt.update(sql, id);
    }

    public int updateOne(String sql, Object object) {
        return npjt.update(sql, new BeanPropertySqlParameterSource(object));
    }

    public int updateOne(String sql, Object... params) {
        return ejt.update(sql, params);
    }

    public <T> List<T> loadOneColumn(String sql, Class<T> clazz, Object... params) {
        return ejt.queryForList(sql, clazz, params);
    }

    public List<Map<String, Object>> loadMapList(String sql, Object... params) {
        return ejt.queryForList(sql, params);
    }

    public JdbcTemplate getJt() {
        return ejt;
    }

    public void setJt(JdbcTemplate jt) {
        this.ejt = jt;
    }

    public NamedParameterJdbcTemplate getNpjt() {
        return npjt;
    }

    public void setNpjt(NamedParameterJdbcTemplate npjt) {
        this.npjt = npjt;
    }
    

	@Override
	public void setPrizeCount(int count,String name) {
		String sql="update t_check "+"set COUNT= ?"+" where NAME=?";
		Object[] params={
				count,name
		};
		ejt.update(sql,params);
		
	}

	@Override
	public int getPrizeCount(String string) {
		String sql="select * from t_check where NAME= ?";
		Object[] params={string};
		RowMapper<Jp> mapper=new JpMapper();
		Jp jp=ejt.queryForObject(sql, params,mapper);
		int count=jp.getCount();
		
		return count;
	}
	

	@Override
	public int insertWinner(String idCard, String mobile,int type,String weixinId,int sid) {
		String  sql="insert into t_winner(IDCARD,MOBILE,TYPE,WEIXINID,SID) values(?,?,?,?,?)";
		Object[] params={idCard,mobile,type,weixinId,sid};
		int c=ejt.update(sql,params);
		return c;
		
	}
	

	@Override
	public int loadFirst(String weixinId) {
		String sql="select count(1) from t_wx where wxid=?";
		Object[] params={weixinId};
	int number=	ejt.queryForInt(sql,params);
	return number;
	}

	
	@Override
	public void insertFirst(String weixinId) {
	String sql="insert into t_wx(WXID) values(?)";
	Object[] params={weixinId};
	ejt.update(sql,params);
		
	}

	@Override
	public int update(String sql, Object... params) {
		return ejt.update(sql,params);
	}

	@Override
	public int getPK() {
		// TODO Auto-generated method stub
		return 0;
	}

	
}
